<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="项目名称">
        <el-input
          v-model="dataForm.projectName"
          placeholder="项目名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="任务名称">
        <el-input
          v-model="dataForm.taskName"
          placeholder="任务名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="任务状态">
        <el-input
          v-model="dataForm.taskStatus"
          placeholder="当前状态"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('business:tasklist:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('business:tasklist:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="任务id">
      </el-table-column>-->
      <!-- <el-table-column
        prop="projectId"
        header-align="center"
        align="center"
        label="项目id">
      </el-table-column>-->
      <el-table-column
        prop="beginTime"
        header-align="center"
        align="center"
        label="计划开始时间"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="计划结束时间"
      ></el-table-column>
      <el-table-column
        prop="taskName"
        header-align="center"
        align="center"
        label="任务名称"
      ></el-table-column>
      <el-table-column
        prop="taskDescription"
        header-align="center"
        align="center"
        label="任务描述"
      ></el-table-column>
      <el-table-column
        prop="dataListing"
        header-align="center"
        align="center"
        label="资料清单"
      ></el-table-column>
      <el-table-column
        prop="allocateUserNames"
        header-align="center"
        align="center"
        label="分配人"
      ></el-table-column>
      <el-table-column
        prop="headUserName"
        header-align="center"
        align="center"
        label="负责人"
      ></el-table-column>
      <el-table-column
        prop="auditUserName"
        header-align="center"
        align="center"
        label="审核人"
      ></el-table-column>
      <el-table-column
        prop="taskStatus"
        header-align="center"
        align="center"
        label="当前状态"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            taskStatusList[scope.row.taskStatus]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('business:tasklist:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.taskId)"
            >修改</el-button
          >
          <el-button
            v-if="isAuth('business:tasklist:update')"
            type="text"
            size="small"
            @click="infoHandle(scope.row.taskId)"
            >详情</el-button
          >
          <el-button
            v-if="isAuth('business:tasklist:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.taskId)"
            >删除</el-button
          >
          <el-button
            v-if="isAuth('business:tasklist:update')"
            type="text"
            size="small"
            @click="updateHandle(scope.row.taskId)"
            >完成</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="queryHandle(scope.row.taskId)"
            >进度</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <task-add-or-update
      v-if="addOrUpdateVisible"
      ref="taskAddOrUpdate"
      @refreshDataList="getDataList"
    ></task-add-or-update>
    <!-- 详情 -->
    <task-list-deil
      v-if="taskListDeil"
      ref="taskListDeil"
      @refreshDataList="getDataList"
    ></task-list-deil>
  </div>
</template>

<script>
import TaskAddOrUpdate from "./task-add-or-update";
import TaskListDeil from "./tasklistdeil";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      taskListDeil: false,
      taskStatusList: {
        1: "进行中",
        2: "已提交",
        3: "延期",
        4: "已完成",
      },
    };
  },
  components: {
    TaskAddOrUpdate,
    TaskListDeil,
  },
  activated() {
    this.getDataList();
  },

  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/business/task/create-task-list"),
        method: "get",
        params: this.$http.adornParams({
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //详情
    infoHandle(id) {
      this.taskListDeil = true;
      this.$nextTick(() => {
        this.$refs.taskListDeil.init(id);
      });
    },
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.taskAddOrUpdate.init(id);
      });
    },
    //进度查看
    queryHandle(id) {
      var map = {};
      map.id = id;
      this.$router.push({
        name: "business-looktask",
        params: { taskinfo: map },
      });
    },
    postDate() {
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      let gettime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
      return gettime;
    },
    //完成
    updateHandle(id) {
      this.$confirm(`确定[id=${id}]任务[${id}完成]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("business/task/update"),
          method: "post",
          data: this.$http.adornData({
            taskId: id,
            taskStatus: 4,
            endTime: this.postDate(),
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.taskId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("business/task/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
